﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;


namespace TractRecord.Code
{
    static public class Common
    {
        public static LogWriter Logger
        {
            get { return EnterpriseLibraryContainer.Current.GetInstance<LogWriter>(); }
        }
        
        public static void Log(string message, string category, int priority, int eventid, System.Diagnostics.TraceEventType eventtype, string title)
        {
            LogEntry l = new LogEntry();
            l.Message = message;
            l.Categories.Add(category);
            l.Priority = priority;
            l.EventId = eventid;
            l.Severity = eventtype;
            
            Log(l);
        }

        public static void Log(string message, string category)
        {
            Log(message, category, 0, 0, System.Diagnostics.TraceEventType.Information, string.Empty);
        }

        public static void Log(string message, string category, int priority)
        {
            Log(message, category, priority, 0, System.Diagnostics.TraceEventType.Information, string.Empty);
        }

        public static void Log(LogEntry le)
        {
            try
            {
                le.ExtendedProperties.Add("User", Membership.GetUser().UserName);
            }
            catch
            {
            }
            Logger.Write(le);
        }


    }

    /// <summary>
    /// A Base36 De- and Encoder
    /// </summary>
    public static class Base36
    {
        private const string CharList = "0123456789abcdefghijklmnopqrstuvwxyz";

        /// <summary>
        /// Encode the given number into a Base36 string
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static String Encode(long input)
        {
            if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");

            char[] clistarr = CharList.ToCharArray();
            var result = new Stack<char>();
            while (input != 0)
            {
                result.Push(clistarr[input % 36]);
                input /= 36;
            }
            return new string(result.ToArray());
        }

        /// <summary>
        /// Decode the Base36 Encoded string into a number
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static Int64 Decode(string input)
        {
            var reversed = input.ToLower().Reverse();
            long result = 0;
            int pos = 0;
            foreach (char c in reversed)
            {
                result += CharList.IndexOf(c) * (long)Math.Pow(36, pos);
                pos++;
            }
            return result;
        }
    }


    public class AttachedFile
    {
        public System.IO.MemoryStream ms;
        public string contenttype;
        public string filename;
    }
}